#import <UIKit/UIKit.h>
#import <CoreMedia/CMSampleBuffer.h>


NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, YHNalUnitType) {
    YHNalUnitType_VPS,
    YHNalUnitType_SPS,
    YHNalUnitType_PPS,
    YHNalUnitType_IDR,
    YHNalUnitType_PFM,
    YHNalUnitType_OTR
};

typedef NS_ENUM(NSUInteger, YHVideoEncodeType) {
    YHVideoEncodeType_H264,
    YHVideoEncodeType_H265
};

@interface YHH26XNalUnit : NSObject

@property (nonatomic, assign, readonly) YHVideoEncodeType encodeType;
@property (nonatomic, assign, readonly) YHNalUnitType type;
@property (nonatomic,   copy, readonly) NSData *rawData;
@property (nonatomic,   copy, readonly) NSData *avccData;

- (instancetype)initWithBuffer:(uint8_t *)buffer bufferSize:(int)bufferSize  encodeType:(YHVideoEncodeType)encodeType;

+ (nullable NSArray<YHH26XNalUnit *> *)parseUnitWithData:(NSData *)data encodeType:(YHVideoEncodeType)encodeType;

@end

@protocol YHVideoDecoderDelegate <NSObject>

@optional

- (void)decoderOutputSampleBuffer:(nullable CMSampleBufferRef)sampleBuffer pixelBuffer:(nullable CVPixelBufferRef)pixelBuffer isDecoded:(BOOL)isDecoded;
- (void)decoderVideoFormatDescriptionChanged:(CMVideoFormatDescriptionRef)videoFormat;
- (void)decoderOutputSnapShot:(UIImage *)image;

@end


typedef void(^DecodeBufferBlock)(CMSampleBufferRef _Nullable sampleBuffer, BOOL isDecoded);
typedef void(^SnapshotImageBlock)(UIImage *_Nullable);

@interface YHVideoDecoder : NSObject

@property (nonatomic, weak) id<YHVideoDecoderDelegate> delegate;

/// 不去解码，使用`AVSampleBufferDisplayLayer`作为渲染前端 ，默认为NOs
@property (nonatomic, assign) BOOL avoidDecode;

- (instancetype)initWithEncodeType:(YHVideoEncodeType)encodeType delegate:(id<YHVideoDecoderDelegate>)delegate;

@property (nonatomic, assign) BOOL reinit;

- (void)decodeWithData:(NSData *)data;

- (void)snapshot;

- (UIImage *)imageWithPixelBuffer:(CVPixelBufferRef)pixelBuffer;

@end

NS_ASSUME_NONNULL_END
